<template>
  <el-dialog title="分配角色" :visible="showCheckbox" @close="btnClose">
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >
        <div>
          {{ item.name }}
        </div>
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnClose">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUesrData } from '@/api/user'
import { PutassignRoles } from '@/api/employees'
export default {
  props: {
    showCheckbox: {
      type: Boolean,
      default: false
    },
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [], // 全部角色
      checkList: [] // 已勾选角色
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList({ page: 1, pagesize: 999 })
      //   console.log(1, rows)
      this.list = rows
    },
    async getUesrData(id) {
      const res = await getUesrData(id)
      //   console.log(id)
      //   console.log(res)
      this.checkList = [...res.roleIds]
    },
    async btnOk() {
      // 发请求
      await PutassignRoles({
        id: this.employeeId,
        roleIds: this.checkList
      })
      this.$message.success('操作成功')
      this.$emit('update:showCheckbox', false)
    },
    btnClose() {
      this.$emit('update:showCheckbox', false)
    }
  }
}
</script>

<style>

</style>
